﻿package com.dLibs.data
{
	import flash.display.MovieClip;
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	import gs.TweenLite;
	
	public class amfObject extends MovieClip
	{
		private var amfConn				:NetConnection;
		private var amfResp				:Responder;
		private var amfFunc				:Function;
		private var amfLoader			:MovieClip;
		private var amfHasLoader		:Boolean;
		private var amfArgsObj			:*;
		
		private var _amfGateULR			:String;
		private var _amfClass			:String;
		
		public function amfObject(amfGateURL:String,amfClass:String,...args):void
		{
			amfHasLoader = false;
			amfResp = new Responder(onResult, onFault);
			amfConn = new NetConnection();
			
			_amfGateULR = amfGateURL;
			_amfClass = amfClass;
			
			if ( typeof args == "array" )
			{
				amfArgsObj = new Array();
				for ( var amfi:uint = 0; amfi < args.length; amfi++ ) amfArgsObj.push(args[amfi]);
			} 
			else if ( typeof args == "object" )
			{
				amfArgsObj = new Object();
				amfArgsObj = args[0];
			}
		}
		
		public function init():void
		{
			amfConn.connect(_amfGateULR);	
			amfConn.call(_amfClass, amfResp, amfArgsObj);
			
			if ( amfHasLoader )
			{
				TweenLite.to(amfLoader,.5,{ alpha : 1 });
			}
		}
		
		/* @ On Receive			________________________________*/
		
		private function onResult(resposta:*):void 
		{
			amfFunc(resposta);
			if ( amfHasLoader )
			{
				TweenLite.to(amfLoader,.5,{ alpha : 0, onComplete : function():void { amfLoader.visible = false; }});
			}
		};
		
		/* @ On ERROR			________________________________*/
		
		private function onFault(resposta:*):void 
		{
			for(var i:String in resposta)
			{
				trace(resposta[i]);
			};
		};
		
		/* @ setLoader			________________________________*/
		
		public function set setLoader(mcLoader:MovieClip):void
		{
			amfLoader 				= mcLoader;			
			amfLoader.visible 		= true;
			amfLoader.alpha 		= 0;
			amfHasLoader 			= true;
		}
		
		/* @ RespFunct			________________________________*/
		
		public function set amfResponder(amfFuncao:Function):void
		{
			amfFunc = amfFuncao;
		}
		
		/* @ End				________________________________*/
	}
}